package my.soft.aiteacher.student.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import my.soft.aiteacher.common.mapper.UserMapper;
import my.soft.aiteacher.student.mapStructs.NoteMapStructs;
import my.soft.aiteacher.student.pojo.NoteDo;
import my.soft.aiteacher.student.mapper.TbNoteMapper;
import my.soft.aiteacher.student.pojo.NoteVo;
import my.soft.aiteacher.student.service.ITbNoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 学生笔记表 服务实现类
 * </p>
 *
 * @author GMCX
 * @since 2025-06-11
 */
@Service
public class TbNoteServiceImpl extends ServiceImpl<TbNoteMapper, NoteDo> implements ITbNoteService {
    @Resource
    private TbNoteMapper tbNoteMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private NoteMapStructs noteMapStructs;

    @Override
    public void uploadNote(NoteVo noteVo) {
        if(Objects.isNull(noteVo.getFileId())){
            throw new RuntimeException("文件id为空");
        }
        if(Objects.isNull(noteVo.getUserId())){
            throw new RuntimeException("用户id为空");
        }
        if(Objects.isNull(userMapper.selectById(noteVo.getUserId()))){
            throw new RuntimeException("用户信息不存在");
        }

        tbNoteMapper.insert(noteMapStructs.toDo(noteVo));
    }

    @Override
    public List<NoteVo> queryNoteList(String userId) {
        if(Objects.isNull(userId)){
            throw new RuntimeException("用户id为空");
        }
        if(Objects.isNull(userMapper.selectById(userId))){
            throw new RuntimeException("用户信息不存在");
        }
        return tbNoteMapper.selectList(new QueryWrapper<NoteDo>().eq("user_id", userId)).stream()
                .map(noteMapStructs::toVo)
                .toList();
    }

    @Override
    public void updateNote(NoteVo noteVo) {
        if(noteVo.getId() == null){
            throw new RuntimeException("笔记id为空");
        }
        tbNoteMapper.updateById(noteMapStructs.toDo(noteVo));
    }
}
